package com.lxw.library.views;

import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;


import com.bumptech.glide.Glide;
import com.lxw.library.R;

import java.util.ArrayList;
import java.util.List;

/**
 * 作者：李学伟
 * 日期：2018/8/9
 * 用途：
 */

public class LxwPhotoView extends LxwBaseView {
    RecyclerView recyclerView;
    RecyclerView.Adapter mAdapter;
    List<String> fileList = new ArrayList<>();

    public LxwPhotoView(final Context context, int orientation, ColumnRequestListener listener) {
        super(context, orientation, listener);
        initView();
    }

    public LxwPhotoView(final Context context, ColumnRequestListener listener) {
        super(context, 0, listener);
        initView();
    }


    private void initView(){
        recyclerView = findViewById(R.id.recyclerView);
        FullyGridLayoutManager manager = new FullyGridLayoutManager(mContext, 3);
        recyclerView.setLayoutManager(manager);
        mAdapter = new RecyclerView.Adapter() {
            @Override
            public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, final int position) {
                View view = LayoutInflater.from(mContext).inflate(R.layout.item_photo, null, false);
                return new RecyclerHolder(view);
            }

            @Override
            public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, final int position) {
                RecyclerHolder holder = (RecyclerHolder) viewHolder;
                if (position == getItemCount() - 1) {
                    holder.setImagePic(R.drawable.add_pic);
                    holder.setOnItemClick(paramer, position, "");
                } else {
                    String filePath = fileList.get(position);
                    holder.setImagePic(filePath);
                    holder.setOnItemClick(paramer, position, filePath);
                }

            }

            @Override
            public int getItemCount() {
                return fileList.size() + 1;
            }
        };

        recyclerView.setAdapter(mAdapter);
    }

    @Override
    public int getHorizontalView() {
        return 0;
    }

    @Override
    public int getVerticalView() {
        return 0;
    }

    public LxwPhotoView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public LxwPhotoView(Context context, int orientation) {
        super(context, orientation);
    }

    public int getViewResourceId() {
        return R.layout.myphotoview_h;
    }


    @Override
    public List<String> getValue() {
        return fileList;
    }

    public void takeSuccess(List<String> valueList) {
        if (valueList.size() != 0) {
            fileList.addAll(valueList);
            mAdapter.notifyDataSetChanged();
        }
    }

    public void takeSuccess(String filePath) {
        fileList.add(filePath);
        mAdapter.notifyDataSetChanged();
    }

    class RecyclerHolder extends RecyclerView.ViewHolder {
        ImageView iv;

        private RecyclerHolder(View itemView) {
            super(itemView);
            iv = (ImageView) itemView.findViewById(R.id.picIv);

        }

        public void setImagePic(String filePath) {
            Glide.with(mContext).load(filePath).into(iv);
        }

        public void setImagePic(int resourceId) {
            Glide.with(mContext).load(resourceId).into(iv);
        }

        public void setOnItemClick(String paramer, int position, String filePath) {
            itemView.setOnClickListener(new OnItemClick(paramer, position, filePath));
        }

        class OnItemClick implements OnClickListener {
            String paramer;
            int position;
            String filePath;

            public OnItemClick(String paramer, int position, String filePath) {
                this.paramer = paramer;
                this.position = position;
                this.filePath = filePath;
            }


            @Override
            public void onClick(View v) {
                onItemClick();
            }

            public void onItemClick() {
                if (position == fileList.size() - 1) {
                    requestListener.selectPhoto(paramer);
                } else {
                    requestListener.showPhoto(paramer, position, filePath);
                }
            }
        }
    }

    @Override
    public boolean checkValueIsLegal() {
        return isMust && fileList.size()!=0;
    }
}
